﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiMovMoviePlayerControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Microsoft.Win32;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands;

public class CUiMovMoviePlayerControl : UserControl
{
  private Button m_PlayButton;
  private Button m_pauseButton;
  private Button m_RunBackwardButton;
  private Button m_runForwardButton;
  private Button m_StopButton;
  private Timer m_movieTimer;
  private Button m_runToEndButton;
  private Button m_RunToStartButton;
  private IContainer components;
  private CApMovDirectXVideoWrapper m_videoWrapper;
  private ToolTip m_toolTipRunToStart;
  private ToolTip m_toolTipRunToEnd;
  private ToolTip m_toolTipPlay;
  private ToolTip m_toolTipPause;
  private ToolTip m_toolTipRewind;
  private ToolTip m_toolTipForward;
  private ToolTip m_toolTipStop;
  private ImageList m_imageListToolBarButtons;
  private TrackBar m_movieTrackBarScene;
  private GroupBox groupBox2;
  private GroupBox groupBox1;
  private Panel m_moviePlayer;
  private double m_FrameKoeficient = 0.1;
  private const string ASSEMBLY_NAME = "DirectX";

  public CUiMovMoviePlayerControl()
  {
    this.InitializeComponent();
    this.DisableContorlButtons();
    this.InitializeToolTipsForPlayMovieButtons();
  }

  protected override void Dispose(bool disposing)
  {
    if (disposing)
    {
      if (this.components != null)
        this.components.Dispose();
      if (this.m_videoWrapper != null)
        this.m_videoWrapper.Dispose();
    }
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    this.components = (IContainer) new System.ComponentModel.Container();
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMovMoviePlayerControl));
    this.m_PlayButton = new Button();
    this.m_pauseButton = new Button();
    this.m_RunBackwardButton = new Button();
    this.m_runForwardButton = new Button();
    this.m_StopButton = new Button();
    this.m_movieTimer = new Timer(this.components);
    this.m_runToEndButton = new Button();
    this.m_RunToStartButton = new Button();
    this.m_toolTipRunToStart = new ToolTip(this.components);
    this.m_toolTipRunToEnd = new ToolTip(this.components);
    this.m_toolTipPlay = new ToolTip(this.components);
    this.m_toolTipPause = new ToolTip(this.components);
    this.m_toolTipRewind = new ToolTip(this.components);
    this.m_toolTipForward = new ToolTip(this.components);
    this.m_toolTipStop = new ToolTip(this.components);
    this.m_imageListToolBarButtons = new ImageList(this.components);
    this.m_movieTrackBarScene = (TrackBar) new TxTrackBar();
    this.groupBox1 = new GroupBox();
    this.groupBox2 = new GroupBox();
    this.m_moviePlayer = new Panel();
    this.m_movieTrackBarScene.BeginInit();
    this.SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_PlayButton, "m_PlayButton");
    this.m_PlayButton.Name = "m_PlayButton";
    this.m_PlayButton.Click += new EventHandler(this.BTN_Play_Click);
    componentResourceManager.ApplyResources((object) this.m_pauseButton, "m_pauseButton");
    this.m_pauseButton.Name = "m_pauseButton";
    this.m_pauseButton.Click += new EventHandler(this.BTN_Pause_Click);
    componentResourceManager.ApplyResources((object) this.m_RunBackwardButton, "m_RunBackwardButton");
    this.m_RunBackwardButton.Name = "m_RunBackwardButton";
    this.m_RunBackwardButton.Click += new EventHandler(this.m_RunBackwardButton_Click);
    componentResourceManager.ApplyResources((object) this.m_runForwardButton, "m_runForwardButton");
    this.m_runForwardButton.Name = "m_runForwardButton";
    this.m_runForwardButton.Click += new EventHandler(this.m_runForwardButton_Click);
    componentResourceManager.ApplyResources((object) this.m_StopButton, "m_StopButton");
    this.m_StopButton.Name = "m_StopButton";
    this.m_StopButton.Click += new EventHandler(this.BTN_Stop_Click);
    this.m_movieTimer.Tick += new EventHandler(this.Movie_Timer_Tick);
    componentResourceManager.ApplyResources((object) this.m_runToEndButton, "m_runToEndButton");
    this.m_runToEndButton.FlatAppearance.BorderSize = 0;
    this.m_runToEndButton.Name = "m_runToEndButton";
    this.m_runToEndButton.Click += new EventHandler(this.m_RunToEndButton_Click);
    componentResourceManager.ApplyResources((object) this.m_RunToStartButton, "m_RunToStartButton");
    this.m_RunToStartButton.Name = "m_RunToStartButton";
    this.m_RunToStartButton.Click += new EventHandler(this.m_RunToStartButton_Click);
    this.m_imageListToolBarButtons.ImageStream = (ImageListStreamer) componentResourceManager.GetObject("m_imageListToolBarButtons.ImageStream");
    this.m_imageListToolBarButtons.TransparentColor = System.Drawing.Color.Transparent;
    this.m_imageListToolBarButtons.Images.SetKeyName(0, "play.ico");
    this.m_imageListToolBarButtons.Images.SetKeyName(1, "pause.ico");
    this.m_imageListToolBarButtons.Images.SetKeyName(2, "stop.ico");
    this.m_imageListToolBarButtons.Images.SetKeyName(3, "start.ico");
    this.m_imageListToolBarButtons.Images.SetKeyName(4, "step back.ico");
    this.m_imageListToolBarButtons.Images.SetKeyName(5, "step.ico");
    this.m_imageListToolBarButtons.Images.SetKeyName(6, "end.ico");
    componentResourceManager.ApplyResources((object) this.m_movieTrackBarScene, "m_movieTrackBarScene");
    this.m_movieTrackBarScene.Maximum = 30;
    this.m_movieTrackBarScene.Name = "m_movieTrackBarScene";
    this.m_movieTrackBarScene.TickStyle = TickStyle.None;
    this.m_movieTrackBarScene.Scroll += new EventHandler(this.m_movieTrackBarScene_Scroll);
    componentResourceManager.ApplyResources((object) this.groupBox1, "groupBox1");
    this.groupBox1.BackColor = SystemColors.Control;
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.TabStop = false;
    componentResourceManager.ApplyResources((object) this.groupBox2, "groupBox2");
    this.groupBox2.Name = "groupBox2";
    this.groupBox2.TabStop = false;
    componentResourceManager.ApplyResources((object) this.m_moviePlayer, "m_moviePlayer");
    this.m_moviePlayer.BorderStyle = BorderStyle.Fixed3D;
    this.m_moviePlayer.Name = "m_moviePlayer";
    this.CausesValidation = false;
    this.Controls.Add((Control) this.groupBox2);
    this.Controls.Add((Control) this.groupBox1);
    this.Controls.Add((Control) this.m_movieTrackBarScene);
    this.Controls.Add((Control) this.m_StopButton);
    this.Controls.Add((Control) this.m_runForwardButton);
    this.Controls.Add((Control) this.m_RunBackwardButton);
    this.Controls.Add((Control) this.m_pauseButton);
    this.Controls.Add((Control) this.m_PlayButton);
    this.Controls.Add((Control) this.m_runToEndButton);
    this.Controls.Add((Control) this.m_RunToStartButton);
    this.Controls.Add((Control) this.m_moviePlayer);
    this.Name = nameof (CUiMovMoviePlayerControl);
    componentResourceManager.ApplyResources((object) this, "$this");
    this.Resize += new EventHandler(this.CUiMovieManagerControl_Resize);
    this.Disposed += new EventHandler(this.CUiMovieManagerControl_Disposed);
    this.m_movieTrackBarScene.EndInit();
    this.ResumeLayout(false);
    this.PerformLayout();
  }

  private void InitializeToolTipsForPlayMovieButtons()
  {
    ResourceManager resourceManager = new ResourceManager("DnProcessSimulateCommands.StringTable", this.GetType().Assembly);
    this.m_toolTipRunToStart.SetToolTip((Control) this.m_RunToStartButton, resourceManager.GetString("MOVIE_MANAGER_MOVIE_RUNTOSTART_TOOLTIP"));
    this.m_toolTipRunToEnd.SetToolTip((Control) this.m_runToEndButton, resourceManager.GetString("MOVIE_MANAGER_MOVIE_RUNTOEND_TOOLTIP"));
    this.m_toolTipPlay.SetToolTip((Control) this.m_PlayButton, resourceManager.GetString("MOVIE_MANAGER_MOVIE_PLAY_TOOLTIP"));
    this.m_toolTipPause.SetToolTip((Control) this.m_pauseButton, resourceManager.GetString("MOVIE_MANAGER_MOVIE_PAUSE_TOOLTIP"));
    this.m_toolTipRewind.SetToolTip((Control) this.m_RunBackwardButton, resourceManager.GetString("MOVIE_MANAGER_MOVIE_REWIND_TOOLTIP"));
    this.m_toolTipForward.SetToolTip((Control) this.m_runForwardButton, resourceManager.GetString("MOVIE_MANAGER_MOVIE_FORWARD_TOOLTIP"));
    this.m_toolTipStop.SetToolTip((Control) this.m_StopButton, resourceManager.GetString("MOVIE_MANAGER_MOVIE_STOP_TOOLTIP"));
  }

  private void InitilizeSliderBar()
  {
    if (this.VideoWrapper != null)
    {
      this.m_movieTrackBarScene.Minimum = 0;
      this.m_movieTrackBarScene.Maximum = Convert.ToInt32(this.VideoWrapper.Duration / (2.0 * this.m_FrameKoeficient));
      this.m_movieTrackBarScene.SmallChange = 1;
      this.m_movieTrackBarScene.Value = 0;
      this.m_movieTrackBarScene.Enabled = true;
    }
    else
      this.InitilizeSliderBarWithDefaults();
  }

  private void InitilizeSliderBarWithDefaults()
  {
    this.m_movieTrackBarScene.Enabled = false;
    this.m_movieTrackBarScene.Minimum = 0;
    this.m_movieTrackBarScene.Maximum = 1;
    this.m_movieTrackBarScene.Value = 0;
  }

  private void CUiMovieManagerControl_Resize(object sender, EventArgs e)
  {
    if (this.VideoWrapper == null)
      return;
    this.VideoWrapper.Resize();
  }

  public bool CreateMovie(string selectedFileAttributePath)
  {
    bool movie;
    try
    {
      this.m_moviePlayer.BackColor = this.m_moviePlayer.BackColor;
      this.m_moviePlayer.Refresh();
      string str = selectedFileAttributePath;
      movie = File.Exists(str);
      if (this.VideoWrapper != null)
      {
        this.VideoWrapper.DisposeVideo();
        this.VideoWrapper = (CApMovDirectXVideoWrapper) null;
      }
      if (movie)
        movie = this.InitializePrevieWithMovie(str);
      this.EnableContorlButtons();
    }
    catch
    {
      movie = false;
      this.InitilizeSliderBarWithDefaults();
    }
    return movie;
  }

  public void RunToStart()
  {
    if (this.VideoWrapper != null)
    {
      for (double num = this.VideoWrapper.CurrentPosition - this.m_FrameKoeficient; num > 0.0 && num < this.VideoWrapper.Duration; num -= this.m_FrameKoeficient)
        this.VideoWrapper.CurrentPosition = num;
      this.VideoWrapper.CurrentPosition = 0.0;
    }
    this.EnableContorlButtons();
    this.UpdateScrollFormCurentPosition();
  }

  public void RunToEnd()
  {
    if (this.VideoWrapper != null)
    {
      for (double num = this.VideoWrapper.CurrentPosition + this.m_FrameKoeficient; num > 0.0 && num < this.VideoWrapper.Duration; num += this.m_FrameKoeficient)
        this.VideoWrapper.CurrentPosition = num;
      this.VideoWrapper.CurrentPosition = this.VideoWrapper.Duration;
    }
    this.EnableContorlButtons();
    this.UpdateScrollFormCurentPosition();
  }

  public void Play()
  {
    if (this.VideoWrapper != null)
    {
      if (this.VideoWrapper.CurrentPosition == this.VideoWrapper.Duration)
        this.VideoWrapper.CurrentPosition = 0.0;
      this.VideoWrapper.Play();
      this.m_movieTimer.Enabled = true;
    }
    this.EnableContorlButtons();
  }

  public void Pause()
  {
    if (this.VideoWrapper != null)
    {
      this.m_movieTimer.Enabled = false;
      this.VideoWrapper.Pause();
    }
    this.EnableContorlButtons();
  }

  public void RunBackward()
  {
    if (this.VideoWrapper != null)
    {
      double duration = this.VideoWrapper.Duration;
      double num = this.VideoWrapper.CurrentPosition - 2.0 * this.m_FrameKoeficient;
      if (num > 0.0)
      {
        this.VideoWrapper.CurrentPosition = num;
        this.EnableContorlButtons();
      }
      else
      {
        this.VideoWrapper.CurrentPosition = 0.0;
        this.EnableContorlButtons();
      }
    }
    this.UpdateScrollFormCurentPosition();
  }

  public void RunForfawd()
  {
    if (this.VideoWrapper == null)
      return;
    double duration = this.VideoWrapper.Duration;
    double num = this.VideoWrapper.CurrentPosition + 2.0 * this.m_FrameKoeficient;
    if (duration > num)
    {
      this.VideoWrapper.CurrentPosition = num;
      this.EnableContorlButtons();
    }
    else
    {
      this.VideoWrapper.CurrentPosition = this.VideoWrapper.Duration;
      this.EnableContorlButtons();
    }
    this.UpdateScrollFormCurentPosition();
  }

  public void Stop()
  {
    if (this.VideoWrapper != null)
    {
      this.m_movieTimer.Enabled = false;
      this.VideoWrapper.Stop();
      this.m_movieTrackBarScene.Value = 0;
    }
    this.EnableContorlButtons();
  }

  private bool InitializeMovie(string filePath)
  {
    bool flag = false;
    try
    {
      int height = this.m_moviePlayer.Height;
      int width = this.m_moviePlayer.Width;
      if (File.Exists(filePath))
      {
        this.VideoWrapper = new CApMovDirectXVideoWrapper((Control) this.m_moviePlayer, filePath);
        this.VideoWrapper.Owner = (Control) this.m_moviePlayer;
        this.m_moviePlayer.Width = width;
        this.m_moviePlayer.Height = height;
        this.VideoWrapper.RegisterEnding(new CApMovMovieManagerDxPlay.DxPlayEvent(this.Video_Ending));
        flag = true;
      }
    }
    catch
    {
      flag = false;
    }
    return flag;
  }

  private ArrayList GetSubKeys(RegistryKey parentKey)
  {
    ArrayList subKeys = new ArrayList();
    foreach (string subKeyName in parentKey.GetSubKeyNames())
      subKeys.Add((object) parentKey.OpenSubKey(subKeyName));
    return subKeys;
  }

  private bool CheckForContatainingName(RegistryKey regKey)
  {
    bool flag = false;
    object obj = regKey.GetValue("ProductName");
    if (obj != null)
    {
      string str = (string) obj;
      if (str != null)
        flag = str.Contains("Microsoft DirectX SDK");
    }
    return flag;
  }

  private void DisposeMedia()
  {
    try
    {
      if (this.VideoWrapper == null)
        return;
      this.VideoWrapper.UnRegisterEnding(new CApMovMovieManagerDxPlay.DxPlayEvent(this.Video_Ending));
      this.VideoWrapper.DisposeVideo();
      this.VideoWrapper = (CApMovDirectXVideoWrapper) null;
      this.m_moviePlayer.BackColor = this.m_moviePlayer.BackColor;
      this.m_moviePlayer.Refresh();
    }
    catch (FileNotFoundException ex)
    {
    }
    finally
    {
      this.DisableContorlButtons();
      this.m_movieTrackBarScene.Value = 0;
      this.m_movieTrackBarScene.Enabled = false;
    }
  }

  public void DisposeAviFile()
  {
    try
    {
      this.DisposeMedia();
    }
    catch (FileNotFoundException ex)
    {
    }
  }

  private void CUiMovieManagerControl_Disposed(object sender, EventArgs e)
  {
    try
    {
      this.DisposeMedia();
    }
    catch (FileNotFoundException ex)
    {
    }
  }

  private void EnableContorlButtons()
  {
    if (this.VideoWrapper != null)
    {
      this.m_StopButton.Enabled = this.VideoWrapper.Playing;
      this.m_PlayButton.Enabled = !this.VideoWrapper.Playing;
      this.m_pauseButton.Enabled = this.VideoWrapper.Playing;
      this.m_RunBackwardButton.Enabled = !this.VideoWrapper.Playing && this.VideoWrapper.CurrentPosition > 0.0;
      this.m_runForwardButton.Enabled = !this.VideoWrapper.Playing && this.VideoWrapper.CurrentPosition < this.VideoWrapper.Duration;
      this.m_runToEndButton.Enabled = !this.VideoWrapper.Playing && this.VideoWrapper.CurrentPosition < this.VideoWrapper.Duration;
      this.m_RunToStartButton.Enabled = !this.VideoWrapper.Playing && this.VideoWrapper.CurrentPosition > 0.0;
    }
    else
      this.DisableContorlButtons();
  }

  private void DisableContorlButtons()
  {
    this.m_StopButton.Enabled = false;
    this.m_PlayButton.Enabled = false;
    this.m_pauseButton.Enabled = false;
    this.m_RunBackwardButton.Enabled = false;
    this.m_runForwardButton.Enabled = false;
    this.m_runToEndButton.Enabled = false;
    this.m_RunToStartButton.Enabled = false;
  }

  private void BTN_Pause_Click(object sender, EventArgs e) => this.Pause();

  private void BTN_Play_Click(object sender, EventArgs e) => this.Play();

  private void BTN_Stop_Click(object sender, EventArgs e) => this.Stop();

  private void m_RunBackwardButton_Click(object sender, EventArgs e) => this.RunBackward();

  private void m_runForwardButton_Click(object sender, EventArgs e) => this.RunForfawd();

  private void m_RunToEndButton_Click(object sender, EventArgs e) => this.RunToEnd();

  private void m_RunToStartButton_Click(object sender, EventArgs e) => this.RunToStart();

  private void m_movieTrackBarScene_Scroll(object sender, EventArgs e)
  {
    if (this.VideoWrapper == null || !this.m_movieTrackBarScene.Enabled)
      return;
    double num = (double) this.m_movieTrackBarScene.Value * this.m_FrameKoeficient;
    if (num >= 0.0 && num <= this.VideoWrapper.Duration)
      this.VideoWrapper.CurrentPosition = (double) this.m_movieTrackBarScene.Value * 2.0 * this.m_FrameKoeficient;
    this.EnableContorlButtons();
  }

  private bool InitializePrevieWithMovie(string downloadedMovieFilePath)
  {
    bool flag;
    if (flag = this.InitializeMovie(downloadedMovieFilePath))
    {
      if (this.VideoWrapper != null)
      {
        this.VideoWrapper.Play();
        this.VideoWrapper.Stop();
      }
      this.InitilizeSliderBar();
    }
    return flag;
  }

  private void Movie_Timer_Tick(object sender, EventArgs e)
  {
    this.UpdateScrollFormCurentPosition();
  }

  private void Video_Ending(object sender, EventArgs e)
  {
    this.VideoWrapper.CurrentPosition = 0.0;
    this.m_movieTrackBarScene.Value = 0;
    this.VideoWrapper.Play();
    this.VideoWrapper.Stop();
    this.m_movieTimer.Enabled = false;
    this.EnableContorlButtons();
  }

  private void UpdateScrollFormCurentPosition()
  {
    if (this.VideoWrapper == null)
      return;
    int int32 = Convert.ToInt32(this.VideoWrapper.CurrentPosition / (2.0 * this.m_FrameKoeficient));
    if (int32 < this.m_movieTrackBarScene.Minimum || int32 > this.m_movieTrackBarScene.Maximum)
      return;
    this.m_movieTrackBarScene.Value = int32;
  }

  internal CApMovDirectXVideoWrapper VideoWrapper
  {
    get => this.m_videoWrapper;
    set => this.m_videoWrapper = value;
  }
}
